<template>
    <h2 style="text-align: center;">等级列表</h2>
    <a href="AddMemberRank" class="btn btn-success">添加等级信息</a><br>
    等级名称:<input type="text" placeholder="请输入等级名称" v-model="name" style="margin-right: 10px;" />
    权益名称<select v-model="MemberRight_Id" style="margin-right: 10px;">
                <option :value="-1">请选择</option>
                <option v-for="item in InfoRight" :value="item.MemberRight_Id">{{ item.MemberRight_Name }}</option>
            </select>
            <input type="button" value="查询" class="btn btn-success" @click="ShowMemberRank"/>
    <table border="1" class="table table-bordered">
        <thead style="background-color: aquamarine;">
            <tr style="text-align: center;">
                <th>等级编号</th>
                <th>等级名称</th>
                <th>等级状态</th>
                <th>权益名称</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody style="text-align: center;">
            <tr v-for="item in Info">
                <td>{{ item.MemberRank_Id }}</td>
                <td>{{ item.MemberRank_Name }}</td>
                <td v-show="item.MemberRank_State==true" style="color: green;">启用</td>
                <td v-show="item.MemberRank_State==false" style="color: red;">禁用</td>
                <td>{{ item.MemberRight_Name }}</td>
                <td>
                    <a @click="UpdRankState(item.MemberRank_Id)" class="btn btn-success">修改状态</a>
                </td>
            </tr>
        </tbody>
    </table>
    <span>
        数据共{{ PageInfo.zongtiao }}条，每页{{ PageInfo.size }}条，共{{ PageInfo.index }}/{{ PageInfo.zongpage }}页

        <a @click="FanYe('F')" class="btn btn-success" style="margin-right: 10px;">首页</a>
        <a @click="FanYe('U')" class="btn btn-success" style="margin-right: 10px;">上一页</a>
        <a @click="FanYe('N')" class="btn btn-success" style="margin-right: 10px;">下一页</a>
        <a @click="FanYe('E')" class="btn btn-success" style="margin-right: 10px;">尾页</a>
    </span>
</template>


<script setup lang="ts">
    import {ref,onMounted} from 'vue';
    import axios from 'axios';

    const Info=ref([{
        MemberRank_Id: 0,
        MemberRank_Name: "",
        MemberRank_State: true,
        MemberRight_Id: -1,
        MemberRight_Name: ""
    }])
    //权益信息
    const InfoRight=ref([{
        MemberRight_Id: 0,
        MemberRight_Name: "",
    }])
    //分页响应式数据
    const PageInfo=ref({
        index:1,
        size:2,
        zongtiao:0,
        zongpage:0
    })
    //翻页
    const FanYe=(chooes:string)=>{
        switch(chooes)
        {
            case"F":
                PageInfo.value.index=1;
            break;
            case"U":
                if(PageInfo.value.index>1)
                {
                    PageInfo.value.index--;
                }
                else
                {
                    alert('当前已是第一页！');
                    return;
                }
            break;
            case"N":
                if(PageInfo.value.index<PageInfo.value.zongpage)
                {
                    PageInfo.value.index++;
                }
                else
                {
                    alert('当前已是最后一页！');
                    return;
                }
            break;
            case"E":
                PageInfo.value.index=PageInfo.value.zongpage;
            break;
        }
        ShowMemberRank();//显示
    }
    onMounted(()=>{
        GetMemberRights();//获取权益
        ShowMemberRank();//显示
    })
    //获取权益
    const GetMemberRights=()=>{
        axios.get("https://localhost:44390/api/MemberRank/GetMemberRights")
        .then(res=>{
            InfoRight.value=res.data
        })
    }
    //显示
    const name=ref("");
    const MemberRight_Id=ref(-1);
    const ShowMemberRank=()=>{
        axios.get("https://localhost:44390/api/MemberRank/ShowMemberRank",
            {
                params:{
                    name:name.value,
                    MemberRight_Id:MemberRight_Id.value,
                    index:PageInfo.value.index,
                    size:PageInfo.value.size
                }
            }
        )
        .then(res=>{
            Info.value=res.data.Plist,
            PageInfo.value.zongpage=res.data.ZongPage,
            PageInfo.value.zongtiao=res.data.ZongTiao
        })
    }

    //修改状态
    const UpdRankState=(MemberRank_Id:number)=>{
        if(confirm("确定修改?")){
            axios.put("https://localhost:44390/api/MemberRank/UpdRankState?id="+MemberRank_Id)
            .then(res=>{
                if(res.data>0)
                {
                    alert('修改成功');
                }
                else
                {
                    alert('修改失败');
                }
            })
        }
    }
</script>